//
//  NewsHandle.m
//  BasketballClub
//
//  Created by David on 16/1/6.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "NewsHandle.h"
#import "NewsModel.h"
#import <AFNetworking.h>
#import "CoreDataManager.h"
static NewsHandle *handle = nil;
@implementation NewsHandle

//数据解析
+(NewsHandle *)sharedNewsHandle
{
    return [[self alloc] init];
}

+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (handle == nil) {
            handle = [super allocWithZone:zone];
        }
    });
    return handle;
}
#pragma mark ---CBA数据解析
-(void)getCBAdataNpc:(NSInteger)numbercba WithBlock:(CBABLOCK)block
{
    ///创建管理类
    AFHTTPSessionManager *sessionManger = [AFHTTPSessionManager manager];
    ///设置返回格式
    sessionManger.responseSerializer = [AFHTTPResponseSerializer serializer];
    ///GET请求
    ///parameters请求参数
    NSString *string = [NSString stringWithFormat:@"http://api.basketball.app887.com/api/Articles.action?keyword=&npc=%ld&opc=20&type=CBA&uid=4467",numbercba];
    [sessionManger GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *dict1 = [dict valueForKey:@"root"];
        
        NSMutableArray *arr1 = [dict1 valueForKey:@"list"];
        
        if (self.CBAarray==nil) {
            self.CBAarray = [NSMutableArray array];
        }

        for (NSDictionary *dic in arr1) {
            NewsModel *model = [[NewsModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.CBAarray addObject:model];
           
            if ([[CoreDataManager shareCoreDateManager] selectAllNewsCBAModel].count >= self.CBAarray.count) {
                [[CoreDataManager shareCoreDateManager] deleteCBANewsAllData];
            }else{
                [[CoreDataManager shareCoreDateManager] insertCBANewsDataToSql:model];
            }
          
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
            block(self.CBAarray.count > 0);
        });
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        self.CBAarray = [NSMutableArray arrayWithArray:[[CoreDataManager shareCoreDateManager] selectAllNewsCBAModel ]];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            block(self.CBAarray.count > 0);
        });
        
    }];
    
}
#pragma NBA数据解析
-(void)getdataWithNPC:(NSInteger)number WithBlock:(BLOCK)block
{
    
    ///创建管理类
    AFHTTPSessionManager *sessionManger = [AFHTTPSessionManager manager];
    
    ///设置返回格式
    sessionManger.responseSerializer = [AFHTTPResponseSerializer serializer];
    ///GET请求
    ///parameters请求参数
    NSString *str = [NSString stringWithFormat:@"http://api.basketball.app887.com/api/Articles.action?keyword=&npc=%ld&opc=20&type=NBA&uid=0",number];
    [sessionManger GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *dict1 = [dict valueForKey:@"root"];
        
        NSMutableArray *arr1 = [dict1 valueForKey:@"list"];
        if (!self.dataArray)
        {
            self.dataArray = [NSMutableArray array];
        }
        
        
        for (NSDictionary *dic in arr1) {
            NewsModel *model = [[NewsModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            model.readarts = [[dic valueForKey:@"readarts"] stringValue];
            
            [self.dataArray addObject:model];
            
            if ([[CoreDataManager shareCoreDateManager] selectAllNewsNBAModel].count > self.dataArray.count) {
                [[CoreDataManager shareCoreDateManager] deleteNBANewsAllData];
            }else{
                [[CoreDataManager shareCoreDateManager] insertNBANewsDataToSql:model];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
            block(self.dataArray.count > 0);
        });
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        self.dataArray = [NSMutableArray arrayWithArray:[[CoreDataManager shareCoreDateManager] selectAllNewsNBAModel]];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            block(self.dataArray.count > 0);
        });
    }];
    
    
}



- (void)loadDataWithWebView:(NSString *)number withBlock:(BLOCK)block
{
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *strUrl = [NSString stringWithFormat:@"http://api.basketball.app887.com/api/GetHtmlCode.action?id=%@",number];
    [sessionManager GET:strUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dic1 = dic[@"root"];
        NSDictionary *dic2 = dic1[@"content"];
        self.WebTitle = dic2[@"title"];
        
        dispatch_async(dispatch_get_main_queue(),^{
            block(self.WebTitle.length > 0);
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}
@end
